package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rm.zeyou.entity.Item;
import com.rm.zeyou.entity.UserCollection;
import com.rm.zeyou.mapper.ItemMapper;
import com.rm.zeyou.mapper.UserCollectionMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.UserCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.vo.request.CollectionRequestVo;
import com.rm.zeyou.vo.response.SelectUserCollectionListResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 收藏表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:09
 */
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService {
    @Resource
    private UserCollectionMapper userCollectionMapper;
    @Resource
    private ItemMapper itemMapper;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ApiRestResponse<Integer> collection(Long userId, CollectionRequestVo requestVo) {
        UserCollection userCollection = userCollectionMapper.selectOne(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId)
                .eq(UserCollection::getItemId, requestVo.getItemId()));
        Integer result;
        if (CheckEmptyUtil.isEmpty(userCollection)) {
            //未收藏  添加收藏
            UserCollection insertUserCollection = new UserCollection();
            insertUserCollection.setUserId(userId);
            insertUserCollection.setItemId(requestVo.getItemId());
            result = userCollectionMapper.insert(insertUserCollection);
        } else {
            //已收藏  取消收藏
            result = userCollectionMapper.cancelCollection(userId, requestVo.getItemId());
        }
        return ApiRestResponse.success(result);
    }

    @Override
    public ApiRestResponse<PageInfo<SelectUserCollectionListResponseVo>> selectUserCollectionList(Long userId, CollectionRequestVo requestVo) {
        PageHelper.startPage(requestVo.getPageNum(), requestVo.getPageSize());
        List<UserCollection> userCollections = userCollectionMapper.selectList(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId)
                .orderByDesc(UserCollection::getCreateTime));
        if (CheckEmptyUtil.isEmpty(userCollections)) {
            return ApiRestResponse.success(new PageInfo<SelectUserCollectionListResponseVo>());
        }

        PageInfo<UserCollection> source = new PageInfo<>(userCollections);
        PageInfo<SelectUserCollectionListResponseVo> target = new PageInfo<>();
        BeanUtils.copyProperties(source, target);

        List<SelectUserCollectionListResponseVo> voList = new ArrayList<>();
        for (UserCollection userCollection : source.getList()) {
            SelectUserCollectionListResponseVo vo = new SelectUserCollectionListResponseVo();
            vo.setUserId(userId);
            vo.setItemId(userCollection.getItemId());
            Item item = itemMapper.selectById(userCollection.getItemId());
            vo.setItemName(item.getItemName());
            vo.setItemMainPicture(item.getItemMainPicture());
            vo.setItemIntroduction(item.getItemIntroduction());
            vo.setItemTime(item.getItemTime()+"");
            vo.setItemPayOrder(item.getRealSalesCount()+item.getVirtualSalesCount());
            vo.setItemPrice(item.getItemPrice());
            vo.setItemMemberPrice(item.getItemMemberPrice());

            voList.add(vo);

        }
        target.setList(voList);

        return ApiRestResponse.success(target);
    }
}
